<?php

//就近 www.jiujin.me
global $_W, $_GPC;
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
load()->func('tpl');
if ($operation == 'display') {
    $starttime = empty($_GPC['time']['start']) ? strtotime('-1 month') : strtotime($_GPC['time']['start']);
    $endtime = empty($_GPC['time']['end']) ? time() : strtotime($_GPC['time']['end']);
    //普通订单
    $normal_list=pdo_fetchall("select o.id,o.ordersn,o.goodsprice,o.createtime,o.paytype,o.paytime,o.status,a.realname,a.mobile,a.province,a.city,a.area,a.address as addre from".tablename("ewei_shop_order")." as o left join ".tablename("ewei_shop_member_address")."as a on o.addressid=a.id left join ".tablename("ewei_shop_member")."as m on a.openid=m.openid where  o.uniacid=:uniacid and o.TotalId!=1 and o.status=:status and o.refundid=:refundid ORDER BY o.createtime DESC", array(":uniacid"=>$_W['uniacid'],":status"=>1,":refundid"=>0));
    foreach($normal_list as $key=>$value){
       $normal_list[$key]['address']=$value['province']." ".$value['city'].' '.$value['area'].' '.$value['addre'];
       unset($normal_list[$key]['province']);
       unset($normal_list[$key]['city']);
       unset($normal_list[$key]['area']);
       unset($normal_list[$key]['addre']);
       $normal_list[$key]['ordertype']="normal_order";
        $normal_order_goods= pdo_fetch('select g.title,g.thumb,og.total,og.optionname from ' . tablename('ewei_shop_order_goods') . ' og ' . ' left join ' . tablename('ewei_shop_goods') . ' g on g.id=og.goodsid ' . ' where og.uniacid=:uniacid and og.orderid=:orderid ', array(':uniacid' => $_W['uniacid'], ':orderid' => $value['id']));
        $normal_list[$key]['title'] = $normal_order_goods['title'];
        $normal_list[$key]['thumb'] = $normal_order_goods['thumb'];
        $normal_list[$key]['total'] = $normal_order_goods['total'];
        $normal_list[$key]['optionname'] = $normal_order_goods['optionname'];
    }
    //微拼团
    $fightgroups_list=pdo_fetchall("select id,orderno as ordersn,price as goodsprice,createtime,ptime as paytime,status,mobile,g_id,addname as realname,address  from ".tablename('tg_order') . " where uniacid=:uniacid and mobile<>'虚拟' and is_hexiao=0 and status=:status ORDER BY createtime DESC",array(":uniacid"=>$_W['uniacid'],":status"=>2));
    foreach ($fightgroups_list as $key => $value){
            $fightgroups_order_goods = pdo_fetch("select gname,gimg from" . tablename('tg_goods') . "where id = '{$value['g_id']}'");
            unset($fightgroups_list[$key]['g_id']);
            $fightgroups_list[$key]['ordertype']="fightgroups_order";
            $fightgroups_list[$key]['title'] = $fightgroups_order_goods['gname'];
            $fightgroups_list[$key]['thumb'] = $fightgroups_order_goods['gimg'];
            $fightgroups_list[$key]['total'] =1;
            $fightgroups_list[$key]['paytype'] =21;
            $fightgroups_list[$key]['optionname']="";
    }
    //限时抢购
    $limittime_list=pdo_fetchall("select id,orderno as ordersn,price as goodsprice,createtime,ptime as paytime,status,mobile,g_id,addname as realname,address  from ".tablename('limittime_order') . " where uniacid=:uniacid and mobile<>'虚拟' and is_hexiao=0 and status=:status ORDER BY createtime DESC",array(":uniacid"=>$_W['uniacid'],":status"=>2));
    foreach ($limittime_list as $key => $value){
            $limittime_order_goods = pdo_fetch("select gname,gimg from" . tablename('limittime_goods') . "where id = '{$value['g_id']}'");
            unset($limittime_list[$key]['g_id']);
            $limittime_list[$key]['ordertype']="limittime_order";
            $limittime_list[$key]['title'] = $limittime_order_goods['gname'];
            $limittime_list[$key]['thumb'] = $limittime_order_goods['gimg'];
            $limittime_list[$key]['total'] =1;
            $limittime_list[$key]['paytype'] =21;
            $limittime_list[$key]['optionname']="";
    }
    //超级团购
    $tuangou_list=pdo_fetchall("select id,orderno as ordersn,price as goodsprice,createtime,ptime as paytime,status,mobile,g_id,addname as realname,address  from ".tablename('tuangou_order') . " where uniacid=:uniacid and mobile<>'虚拟' and is_hexiao=0 and status=:status ORDER BY createtime DESC",array(":uniacid"=>$_W['uniacid'],":status"=>2));
    foreach($tuangou_list as $key => $value){
            $tuangou_order_goods = pdo_fetch("select gname,gimg from" . tablename('tuangou_goods') . "where id = '{$value['g_id']}'");
            unset($tuangou_list[$key]['g_id']);
            $tuangou_list[$key]['ordertype']="tuangou_order";
            $tuangou_list[$key]['title'] = $tuangou_order_goods['gname'];
            $tuangou_list[$key]['thumb'] = $tuangou_order_goods['gimg'];
            $tuangou_list[$key]['total'] =1;
            $tuangou_list[$key]['paytype'] =21;
            $tuangou_list[$key]['optionname']="";
    }
    //拼团抽奖
    $lottery_list=pdo_fetchall("select id,orderno as ordersn,price as goodsprice,createtime,ptime as paytime,status,mobile,g_id,addname as realname,address  from ".tablename('lottery_order') . " where uniacid=:uniacid and mobile<>'虚拟' and is_hexiao=0 and status=:status and islottery=:islottery ORDER BY createtime DESC",array(":uniacid"=>$_W['uniacid'],":status"=>2,':islottery'=>0));
    foreach($lottery_list as $key => $value){
            $lottery_order_goods = pdo_fetch("select gname,gimg from" . tablename('lottery_goods') . "where id = '{$value['g_id']}'");
            unset($lottery_list[$key]['g_id']);
            $lottery_list[$key]['ordertype']="lottery_order";
            $lottery_list[$key]['title'] = $lottery_order_goods['gname'];
            $lottery_list[$key]['thumb'] = $lottery_order_goods['gimg'];
            $lottery_list[$key]['total'] =1;
            $lottery_list[$key]['paytype'] =21;
            $lottery_list[$key]['optionname']="";
    }
    //积分兑换
    $creditshop_list = pdo_fetchall("select log.id,log.logno as ordersn,log.realname,g.price as goodsprice,log.createtime,log.usetime as paytime,log.status,log.mobile,g.title,g.thumb,log.province,log.city,log.area,log.address as addre from " . tablename('ewei_shop_creditshop_log') . " log " . " left join  " . tablename('ewei_shop_creditshop_goods') . " g on g.id = log.goodsid" ." where log.uniacid=:uniacid and log.status=:status order by log.createtime desc",array(":uniacid"=>$_W['uniacid'],":status"=>2));
    foreach($creditshop_list as $key=>$value){
            $creditshop_list[$key]['address']=$value['province']." ".$value['city'].' '.$value['area'].' '.$value['addre'];
            unset($creditshop_list[$key]['province']);
            unset($creditshop_list[$key]['city']);
            unset($creditshop_list[$key]['area']);
            unset($creditshop_list[$key]['addre']);
            $creditshop_list[$key]['ordertype']="creditshop_order";
            $creditshop_list[$key]['total'] =1;
            $creditshop_list[$key]['paytype'] =21;
            $creditshop_list[$key]['optionname']="";
    }
    //金币兑换
    $goldshop_list = pdo_fetchall("select log.id,log.logno as ordersn,log.realname,g.price as goodsprice,log.createtime,log.usetime as paytime,log.status,log.mobile,g.title,g.thumb,log.dispatchstatus,log.dispatchno,log.province,log.city,log.area,log.address as addre from " . tablename('ewei_shop_goldshop_log') . " log " . " left join  " . tablename('ewei_shop_goldshop_goods') . " g on g.id = log.goodsid" ." where log.uniacid=:uniacid and log.status=:status and log.paystatus=:paystatus  and log.creditpay=:creditpay order  by log.createtime desc",array(":uniacid"=>$_W['uniacid'],":status"=>2,":paystatus"=>1,":creditpay"=>1));
    foreach($goldshop_list as $key=>$value){
            if($value['dispatchstatus']==1 && !empty($value['dispatchno'])){
            $goldshop_list[$key]['ordertype']="goldshop_order";
            $goldshop_list[$key]['total'] =1;
            $goldshop_list[$key]['paytype'] =21;
            $goldshop_list[$key]['address']=$value['province']." ".$value['city'].' '.$value['area'].' '.$value['addre'];
            $goldshop_list[$key]['optionname']="";
            unset($goldshop_list[$key]['province']);
            unset($goldshop_list[$key]['city']);
            unset($goldshop_list[$key]['area']);
            unset($goldshop_list[$key]['addre']);
            unset($goldshop_list[$key]['dispatchstatus']);
            unset($goldshop_list[$key]['dispatchno']);
            }elseif($value['dispatchstatus']==-1 && empty($value['dispatchno'])){
            $goldshop_list[$key]['ordertype']="goldshop_order";
            $goldshop_list[$key]['total'] =1;
            $goldshop_list[$key]['paytype'] =21;  
            $goldshop_list[$key]['address']=$value['province']." ".$value['city'].' '.$value['area'].' '.$value['addre'];
            $goldshop_list[$key]['optionname']="";
            unset($goldshop_list[$key]['province']);
            unset($goldshop_list[$key]['city']);
            unset($goldshop_list[$key]['area']);
            unset($goldshop_list[$key]['addre']);
            unset($goldshop_list[$key]['dispatchstatus']);
            unset($goldshop_list[$key]['dispatchno']);
            }else{
                unset($goldshop_list[$key]);
            }
    }
    $list=array_merge($normal_list,$fightgroups_list,$limittime_list,$tuangou_list,$lottery_list,$goldshop_list);
        //导出订单
        if ($_GPC['export'] == 1) {
            $columns = array(
                array('title' => '订单编号', 'field' => 'ordersn', 'width' => 24),
                array('title' => '商品名称', 'field' => 'title', 'width' =>50),
                array('title' => '规格及编码', 'field' => 'optionname', 'width' => 12),
                array('title' => '商品数量', 'field' => 'total', 'width' => 12),
                array('title' => '姓名', 'field' => 'realname', 'width' => 12),
                array('title' => '手机号码', 'field' => 'mobile', 'width' => 12),
                array('title' => '收货地址', 'field' => 'address', 'width' =>50),
                array('title' => '下单时间', 'field' => 'createtime', 'width' =>24),
                array('title' => '付款时间', 'field' => 'paytime', 'width' =>24),
                array('title' => '订单类型', 'field' => 'ordertype', 'width' => 12),
                array('title' => '状态', 'field' => 'status', 'width' => 12)
            );
            foreach($list as $k=>$v){
                $list[$k]['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
                $list[$k]['paytime'] = date('Y-m-d H:i:s', $v['paytime']);
                $list[$k]['title']=$v['title'];
                $list[$k]['addresstype']=$v['addresstype']==1?'公司':'家庭';
                $list[$k]['ordersn']=" ".$v['ordersn'];
                if($v['ordertype']=="normal_order"){
                $list[$k]['ordertype']="普通订单";
                }
                if($v['ordertype']=="fightgroups_order"){
                $list[$k]['ordertype']="微拼团订单";
                }
                if($v['ordertype']=="limittime_order"){
                $list[$k]['ordertype']="限时抢购订单";
                }
                if($v['ordertype']=="tuangou_order"){
                $list[$k]['ordertype']="团购订单";
                }
                if($v['ordertype']=="lottery_order"){
                $list[$k]['ordertype']="拼团抽奖直购订单";
                }
                if($v['ordertype']=="creditshop_order"){
                $list[$k]['ordertype']="积分商城订单";
                }
                if($v['ordertype']=="goldshop_order"){
                $list[$k]['ordertype']="金币商城订单";
                }
                $list[$k]['status']="待发货";
            }
            m('excel')->export($list, array("title" => "全部待发货订单导出" . date('Y-m-d-H-i', time()), "columns" => $columns));
        }
    include $this->template('web/allorder/allorder_list');
    die;
}